package com.yn.pet.controller.app;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.yn.common.core.domain.AjaxResult;
import com.yn.pet.controller.app.biz.AlarmRecordBizService;
import com.yn.pet.controller.pc.biz.DeviceInfoBizService;
import com.yn.pet.controller.pc.request.FenceAlarmRecordRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Set;

@RestController
@AllArgsConstructor
@RequestMapping("/front/alarm")
@Api(tags = "app端——警告记录Controller")
@Slf4j
public class AlarmRecordController {
    private final AlarmRecordBizService alarmRecordBizService;
    private final DeviceInfoBizService deviceInfoBizService;


    /**
     * 获取围栏围栏告警记录
     * @param request 请求参数
     * @return IPage<FenceAlarmQueryResponse>
     */
    @GetMapping("/fence/pageList")
    @ApiOperation("获取围栏报警信息列表")
    public AjaxResult listFenceAlarmRecord(FenceAlarmRecordRequest request) {
        log.info("获取围栏告警记录入参，request：{}",request);


        return AjaxResult.success(alarmRecordBizService.listFenceAlarmRecord(request));
    }

    /**
     * 删除围栏告警记录
     * @param alarmId  警告id
     * @return
     */
    @PostMapping("fence/delete/{alarmId}")
    @ApiOperation("删除围栏告警记录")
    public AjaxResult deleteFenceAlarmRecord(@PathVariable @NotNull @ApiParam("围栏警告id") Long alarmId) {
        log.info("删除围栏告警记录入参，alarmId：{}",alarmId);

        alarmRecordBizService.removeFenceAlarmRecord(alarmId);
        return AjaxResult.success("删除围栏告警记录成功");
    }

    /**
     * 批量删除围栏告警记录
     * @param alarmIdSet 围栏告警id集合
     * @return
     */
    @PostMapping("fence/deleteBatch")
    @ApiOperation("批量删除围栏告警记录")
    public AjaxResult deleteBatchFenceAlarmRecord(@RequestBody @ApiParam("围栏警告id集合") Set<Long> alarmIdSet){
        log.info("批量删除围栏告警记录入参，alarmIdSet：{}",alarmIdSet);
        if(CollectionUtils.isEmpty(alarmIdSet)){
            throw new RuntimeException("请选择要删除的围栏告警记录");
        }
        alarmRecordBizService.removeFenceAlarmRecordBatch(alarmIdSet);
        return AjaxResult.success("批量删除围栏告警记录成功");
    }



    /**
     * 获取其他警告记录列表
     * @param request 请求参数
     * @return IPage<OtherAlarmQueryResponse>
     */
    @GetMapping("/other/pageList")
    @ApiOperation("获取其他报警信息列表")
    public AjaxResult listOtherAlarmRecord(FenceAlarmRecordRequest request) {
        log.info("获取其他告警记录入参，request：{}",request);

        return AjaxResult.success(alarmRecordBizService.listOtherAlarmRecord(request));
    }

    /**
     * 删除其他告警记录
     * @param id  其他警告id
     * @return
     */
    @PostMapping("other/delete/{id}")
    @ApiOperation("删除其他告警记录")
    public AjaxResult deleteOtherAlarmRecord(@PathVariable @NotNull @ApiParam("其他警告id") Long id) {
        log.info("删除其他告警记录入参，alarmId：{}",id);

        alarmRecordBizService.removeOtherAlarmRecord(id);
        return AjaxResult.success("删除告警记录成功");
    }

    /**
     * 批量删除其他告警记录
     * @param idSet 其他告警id集合
     * @return
     */
    @PostMapping("other/deleteBatch")
    @ApiOperation("批量删除其他告警记录")
    public AjaxResult deleteBatchOtherAlarmRecord(@RequestBody @ApiParam("围栏警告id集合") Set<Long> idSet){
        log.info("批量删除其他告警记录入参，alarmIdSet：{}",idSet);
        if(CollectionUtils.isEmpty(idSet)){
            return AjaxResult.warn("请选择要删除的告警记录");
        }
        alarmRecordBizService.removeOtherAlarmRecordBatch(idSet);
        return AjaxResult.success("批量删除告警记录成功");
    }



    @GetMapping("searchDeviceList")
    @ApiOperation("用户端搜索设备列表")
    public AjaxResult searchDeviceList(@RequestParam("keyword") @NotBlank String keyword) {
        log.info("用户端搜索设备列表入参，keyword:{}", keyword);
        return AjaxResult.success(deviceInfoBizService.searchDeviceList(keyword));
    }
}
